<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit|ie-comp|ie-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <title>难易系数配置</title>
</head>

<body>
  <div id="app" v-loading.fullscreen.lock="loading">
    <div class="facility-level-setting">
      <search-btn-view btn-text="查询" @before-open="resetSearch" @search="searchData">
        <template slot="append">
          <el-button size="medium" type="primary" @click="addLevel">新增难易系数</el-button>
        </template>
        <div slot="search-content">
          <search-form :search="search"></search-form>
        </div>
      </search-btn-view>
      <div class="facility-level-table-div">
        <el-table fit stripe border :data="facilityLevelList" style="width: 100%" height="parent">
          <el-table-column label="等级" prop="rank" min-width="120px" align="center">
          </el-table-column>
          <el-table-column label="难易度" prop="difficultLevelRate" min-width="120px" align="center">
            <template slot-scope="scope">
              <span>{{scope.row.difficultLevelRate}}%</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="状态" prop="status" width="80px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg('bomBusiness.status.'+scope.row.status)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="操作" width="80px" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="updateLevel(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="margin-top:5px;text-align:right" v-if="pagination.totalCount!==0">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount" :current-page.sync="pagination.currentPage"
          @current-change="pageChange"></el-pagination>
      </div>
    </div>

    <el-dialog :title="formType==='add'?'新增难易系数':'修改难易系数'" :visible.sync="openForm" width="500px" top="calc(50vh - 130px)"
      @closed="formClosed">
      <div style="height:200px;overflow:auto">
        <facility-level-form v-if="openForm" :level="selectFacilityLevel" :type="formType" @save-success="saveSuccess">
        </facility-level-form>
      </div>
    </el-dialog>
  </div>
</body>

<script src="../../myJs/importFile.js"></script>
<script>
  importFile([
    "/vueSystem/components/css/searchBtnView.css",
    "./css/facilityLevelForm.css"
  ], [
    "/vueSystem/i18n/index.js",
    "/vueSystem/components/searchBtnView.js",
    "/vueSystem/models/baseModel.js"
  ]);

</script>
<script src="./searchForm.js"></script>
<script src="./facilityLevelForm.js"></script>

<script>
  var app = new Vue({
    el: '#app',
    data: function () {
      var search = {
        status: ''
      };
      return {
        loading: false,
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        facilityLevelList: [],
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 15
        },
        openForm: false,
        formType: '',
        selectFacilityLevel: null
      };
    },

    created: function () {
      this.setFacilityLevelList(true);
    },

    methods: {
      setFacilityLevelList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var postData = {};
        var search = this.search
        var status = search.status;
        if (status !== '') {
          postData.status = status;
        }

        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        postData.start = start;
        postData.length = pageSize;

        var search = this.search;
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/getDifficultLevelRateList',postData).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          var facilityLevelList = res.returnData;
          for (var i = 0; i < facilityLevelList.length; i++) {
            var facilityLevel = facilityLevelList[i];
            var difficultLevelRate = facilityLevel.difficultLevelRate;
            if (difficultLevelRate !== null) {
              difficultLevelRate = _.round(difficultLevelRate * 100, 1);
            } else {
              difficultLevelRate = undefined;
            }
            facilityLevel.difficultLevelRate = difficultLevelRate;
          }

          self.facilityLevelList = facilityLevelList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
        }).catch(function(){
          self.loading = false;
        });
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.pagination.currentPage = 1;
        this.setFacilityLevelList(true);
      },

      pageChange:function(){
        this.setFacilityLevelList();
      },

      addLevel: function () {
        this.formType = 'add';
        this.openForm = true;
      },

      updateLevel:function(facilityLevel){
        this.selectFacilityLevel = facilityLevel;
        this.formType = 'update';
        this.openForm = true;
      },

      saveSuccess: function () {
        this.openForm = false;
        this.setFacilityLevelList();
      },

      formClosed: function () {
        this.formType = '';
        this.selectFacilityLevel = null;
      }
    },

    components: {
      SearchBtnView: SearchBtnView,
      SearchForm: SearchForm,
      FacilityLevelForm: FacilityLevelForm
    }
  });

</script>

<style>
  .facility-level-setting {
    padding: 10px 20px 5px;
    height: calc(100% - 15px);
    display: flex;
    flex-direction: column;
  }

  .facility-level-setting .facility-level-table-div{
    flex: 1;
    overflow: auto;
  }

</style>

</html>
